package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.NoticeMessageTagService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.NoticeMessageTagDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-11-13 17:02
 * @Description:
 */
@RestController
@RequestMapping("/api/noticeMessageTag")
public class NoticeMessageTagController extends UserInfoController {
    @Autowired
    private NoticeMessageTagService noticeMessageTagService;

    @GetMapping("/findByUserIdAndCurrentList")
    public ServerResponse findByUserIdAndCurrentList(HttpServletRequest request) {
        NoticeMessageTagDto noticeMessageTagDto = new NoticeMessageTagDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
//        noticeMessageTagDto.setUserInfo(userInfo);
        if(RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())){
            noticeMessageTagDto.setUserId(userInfo.getParentId());
        }else {
            noticeMessageTagDto.setUserId(userInfo.getId());
        }

        return ServerResponse.createBySuccess(noticeMessageTagService.findByUserIdAndCurrentList(noticeMessageTagDto));
    }
    @PostMapping("/edit")
    public ServerResponse edit(@RequestBody NoticeMessageTagDto noticeMessageTagDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
//        noticeMessageTagDto.setUserId(userInfo.getId());
        if(RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())){
            noticeMessageTagDto.setUserId(userInfo.getParentId());
        }else {
            noticeMessageTagDto.setUserId(userInfo.getId());
        }
        return noticeMessageTagService.edit(noticeMessageTagDto);
    }
    @DeleteMapping("/delete")
    public ServerResponse delete(@RequestBody NoticeMessageTagDto noticeMessageTagDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
//        noticeMessageTagDto.setUserId(userInfo.getId());
        if(RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())){
            noticeMessageTagDto.setUserId(userInfo.getParentId());
        }else {
            noticeMessageTagDto.setUserId(userInfo.getId());
        }
        return noticeMessageTagService.delete(noticeMessageTagDto);
    }
}
